package com.project.weather.aspectj;

import com.project.weather.common.Constant;
import com.project.weather.common.ResultMap;
import com.project.weather.pojo.SearchCityBean;
import com.project.weather.pojo.UserBean;
import com.project.weather.pojo.api.WeatherLiveResponsePojo;
import com.project.weather.pojo.api.WeatherSevendaysResponsePojo;
import com.project.weather.service.SearchCityService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Aspect
@Configuration
public class SearchCityAspect {

    private static final Logger logger = LoggerFactory.getLogger(SearchCityAspect.class);

    @Autowired
    private SearchCityService searchCityService;

    /**
     * 定义切入点，切入点为com.cobo.learn.controller下的所有函数
     */
    @Pointcut("execution(public * com.project.weather.controller.WeatherController.*(..))")
    public void webLog(){}


    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "webLog()", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
        logger.info("第一个后置返回通知的返回值："+jsonResult);

        //用的最多 通知的签名
        Signature signature = joinPoint.getSignature();
        //代理的是哪一个方法
        logger.info("代理的是哪一个方法"+signature.getName());

        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpSession session = attr.getRequest().getSession(true);

        // 获取当前的用户
        UserBean loginUser = (UserBean) session.getAttribute(Constant.CURRENT_ACCOUNT);

        if(loginUser == null){
            return;
        }


        try {

            SearchCityBean searchCityBean = new SearchCityBean();

            ResultMap resultMap = (ResultMap) jsonResult;
            Object result = resultMap.getResult();

            String name = signature.getName();

            if("searchLive".equals(name)){

                if(result instanceof WeatherLiveResponsePojo){

                    WeatherLiveResponsePojo weatherLiveResponsePojo = (WeatherLiveResponsePojo) result;

                    searchCityBean.setCity_id(weatherLiveResponsePojo.getCityid());
                    searchCityBean.setCity_name(weatherLiveResponsePojo.getCity());

                }


            }else if("searchSevenDays".equals(name)){
                if(result instanceof WeatherSevendaysResponsePojo){

                    WeatherSevendaysResponsePojo weatherSevendaysResponsePojo = (WeatherSevendaysResponsePojo) result;
                    searchCityBean.setCity_id(weatherSevendaysResponsePojo.getCityid());
                    searchCityBean.setCity_name(weatherSevendaysResponsePojo.getCity());

                }
            }

            if(loginUser!=null){
                searchCityBean.setUser_id(loginUser.getUser_id());
            }

            searchCityBean.setSearch_time(new Date());

            searchCityService.insert(searchCityBean);

        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }





    }

}
